package com.dove.controller;

import com.dove.mapper.UserMapper;
import com.dove.entity.User;
import com.dove.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@RestController
//@RequestMapping("/users")
public class UserController2 {
    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;

    //更新或者新增
    @PostMapping
    public Boolean save(@RequestBody User user) {
        return userService.save(user);
    }

    @DeleteMapping("{id}")
    public Boolean remove(@PathVariable Integer id) {
        return userService.delete(id);
    }

    @PutMapping
    public Boolean modify(@RequestBody User user) {
        return userService.update(user);
    }

    @GetMapping
    public List<User> findAll() {
        List<User> userList = userService.getAll();
        return userList;
    }

    @GetMapping("{id}")
    public User findById(@PathVariable Integer id) {
        return userService.getById(id);
    }

    //select * from sys_user limit 0,5
    @GetMapping("/page")
    public Map<String, Object> getPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        pageNum = (pageNum - 1) * pageSize;
        Integer pages = userMapper.selectTotal();
        List<User> data = userMapper.mySelectPage(pageNum, pageSize);
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("pages", pages);
        return res;
    }
}
